
const regObj = {
    regUserName: /^[a-zA-Z0-9_@.-]{6,16}$/, // 6-16位大小写字母、数字、特殊符号
    regPassword: /^[a-zA-Z0-9]{6,20}$/, // 6-20位大小写字母、数字、符号 去掉@\$\^\.\*\\?
    regName: /[\u4E00-\u9FA5\uf900-\ufa2da-zA-Z· ]{1,48}$/, // 校验姓名
    regTel: /^[1][3,4,5,6,7,8,9][0-9]{9}$/, // 校验手机号
    regTelNum: /^[0-9\-]{6,15}$/, // 校验电话号码
    regEmail: /^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$/, // 校验邮箱
    regCompanyName: /^([\u4E00-\u9FA5\uf900-\ufa2da-z\.\s\,\(\)（）0-9]){4,50}$/i, // 校验公司名字
    regInvoiceCode: /^[A-Z0-9]{15}$|^[A-Z0-9]{18}$|^[A-Z0-9]{20}$/, // 校验纳税识别号
    regBankCount: /^([1-9]{1})(\d{15}|\d{17}|\d{18})$/ // 校验银行卡号
}

let checkTFObj = {}

for (let k in regObj) {
    let _str = 'check' + k.substring(3) + 'R'
    checkTFObj[_str] = (value) => {
        let _value = value.trim()
        if (_value.length != 0) {
            if (!regObj[k].test(_value)) {
                return false
            } else {
                return true
            }
        } else {
            return true
        }
    }
}

export const checkNameR = (value) => {
    let _value = value.trim()
    if (_value.length != 0) {
        let _reg = /^[\u4E00-\u9FA5\uf900-\ufa2da-zA-Z]{1}[\u4E00-\u9FA5\uf900-\ufa2da-zA-Z·]{1,20}$/
        if (!_reg.test(_value)) {
            return false
        } else {
            return true
        }
    } else {
        return true
    }
}

// 校验登录名
export const checkUserName = (rule, value, callback) => {
    let _value = value//.trim()
    if (_value.length != 0) {
        if (!regObj.regUserName.test(_value)) {
            callback(new Error('登录名格式不正确'))
        } else {
            callback()
        }
    } else {
        callback()
    }
}
// 校验姓名
export const checkName = (rule, value, callback) => {
    let _value = value.trim()
    if (_value.length != 0) {
        if (!regObj.regName.test(_value)) {
            callback(new Error('请输入2到20位的合法姓名'))
        } else {
            callback()
        }
    } else {
        callback()
    }
}


// 校验手机号
export const checkTel = (rule, value, callback) => {
    let _value = value.trim()
    if (_value.length != 0) {
        if (!regObj.regTel.test(_value)) {
            callback(new Error('请输入合法的手机号码'))
        } else {
            callback()
        }
    } else {
        callback()
    }
}

// 校验电话号码
export const checkTelNum = (rule, value, callback) => {
    let _value = value.trim()
    if (_value.length != 0) {
        if (!regObj.regTelNum.test(_value)) {
            callback(new Error('请输入合法的电话号码'))
        } else {
            callback()
        }
    } else {
        callback()
    }
}

// 校验邮箱
export const checkEmail = (rule, value, callback) => {
    let _value = value.trim()
    if (_value.length != 0) {
        if (!regObj.regEmail.test(_value)) {
            callback(new Error('请输入合法的邮箱'))
        } else {
            callback()
        }
    } else {
        callback()
    }
}

// 校验公司名字
export const checkCompanyName = (rule, value, callback) => {
    let _value = value
    if (_value.length != 0) {
        // let _reg = /^(([\u4e00-\u9fff]{4,50})|([a-z\.\s\,\(\)（）]{8,50}))$/i;
        if (!regObj.regCompanyName.test(_value)) {
            callback(new Error('企业名称应符合规则，不少于4个汉字'))
        } else {
            callback()
        }
    } else {
        callback()
    }
}

/** function _checkAddressCode(addressCode){
    var provinceAndCitys={11:"北京",12:"天津",13:"河北",14:"山西",15:"内蒙古",21:"辽宁",22:"吉林",23:"黑龙江",
        31:"上海",32:"江苏",33:"浙江",34:"安徽",35:"福建",36:"江西",37:"山东",41:"河南",42:"湖北",43:"湖南",44:"广东",
        45:"广西",46:"海南",50:"重庆",51:"四川",52:"贵州",53:"云南",54:"西藏",61:"陕西",62:"甘肃",63:"青海",64:"宁夏",
        65:"新疆",71:"台湾",81:"香港",82:"澳门",91:"国外"};
    var check = /^[1-9]\d{5}$/.test(addressCode);
    if(!check) return false;
    if(provinceAndCitys[parseInt(addressCode.substring(0,2))]){
        return true;
    }else{
        return false;
    }
}
 
function _isValidOrgCode(value){
    if(value!=""){
        var part1=value.substring(0,8);
        var part2=value.substring(value.length-1,1);
        var ws = [3, 7, 9, 10, 5, 8, 4, 2];
        var str = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
        var reg = /^([0-9A-Z]){8}$/;
        if (!reg.test(part1))
        {
            return true
        }
        var sum = 0;
        for (var i = 0; i< 8; i++)
        {
            sum += str.indexOf(part1.charAt(i)) * ws[i];
        }
        var C9 = 11 - (sum % 11);
        var YC9=part2+'';
        if (C9 == 11) {
            C9 = '0';
        } else if (C9 == 10) {
            C9 = 'X' ;
        } else {
            C9 = C9+'';
        }
        return YC9!=C9;
    }
} **/

// 校验纳税号
export const checkInvoiceCode = (rule, value, callback) => {
    console.log(callback)
    let _value = value.trim()
    if (_value.length != 0) {
        if (!regObj.regInvoiceCode.test(_value)) {
            callback(new Error('请填写正确的纳税号码'))
        } else {
            callback()
        }
    } else {
        callback()
    }
}

// 非EL
checkTFObj.checkInvoiceCodeR = (value) => {
    let _value = value.trim()
    if (_value.length != 0) {
        let _reg = /^[A-Z0-9]{15}$|^[A-Z0-9]{18}$|^[A-Z0-9]{20}$/;
        if (!_reg.test(_value)) {
            return '请填写正确的纳税号码'
        } else {
            return true
        }
    } else {
        return true
    }
}

// 校验银行卡号
export const checkBankCount = (rule, value, callback) => {
    let _value = value.trim()
    if (_value.length != 0) {
        if (!regObj.regBankCount.test(_value)) {
            callback(new Error('请输入正确的银行卡号'))
        } else {
            callback()
        }
    } else {
        callback()
    }
}
// 正则表达式
export const regAll = regObj;
export let checkTF = checkTFObj;